//
//  MBOrderViewController.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/19.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBOrderViewController.h"
#import "MBGroupOrderViewController.h"
#import "MBOrderNaviView.h"
#import "MBOrderTypeView.h"
#import "MBOrderGoodsTableViewCell.h"
#import "MBDeliverGoodsPopView.h"
#import "MBOrderViewModel.h"
#import "MBGroupOrderTypeView.h"

@interface MBOrderViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)MBDeliverGoodsPopView* popView;

@property (nonatomic,weak)MBOrderTypeView* typeView;
@property (nonatomic,weak)MBOrderNaviView* orderNaviView;
@property (nonatomic,weak)MBGroupOrderTypeView* orderTypeView;
@property (nonatomic,assign)BOOL isShowOrderType;
@property (nonatomic,weak)UIView* shadowView;

@end

@implementation MBOrderViewController
@dynamic viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupLayout];
    
    [self racValue];
}

-(void)setupLayout
{
    [super setupLayout];
    
    MBOrderNaviView* naviView = [[MBOrderNaviView alloc]init];
    self.orderNaviView = naviView;
    naviView.searchTextField.delegate = self;
    [self.naviView addSubview:naviView];
    
    [naviView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsZero);
    }];
    
    MBOrderTypeView* typeView = [[MBOrderTypeView alloc]init];
    self.typeView = typeView;
    [self.view addSubview:typeView];
    
    [typeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(SCREEN_STATUS_BAR_HEIGHT+44);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo([UIView lf_sizeFromIphone6:44]);
    }];
    
    [self setUpMjRefresh];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(typeView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    
    MBDeliverGoodsPopView* popView = [[MBDeliverGoodsPopView alloc]init];
    self.popView = popView;
    
    UIView* shadowView = [[UIView alloc]init];
    self.shadowView = shadowView;
    [shadowView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenOrderTypeView)]];
    shadowView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    self.shadowView.hidden = YES;
    [self.view addSubview:shadowView];
    
    [shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.tableView).insets(UIEdgeInsetsZero);
    }];
    
    MBGroupOrderTypeView* orderTypeView = [[MBGroupOrderTypeView alloc]initWithFrame:CGRectMake(0, 0, (SCREEN_WIDTH-[UIView lf_sizeFromIphone6:30])/3+[UIView lf_sizeFromIphone6:15], [UIView lf_sizeFromIphone6:35]*7) andType:@[@"全部订单",@"待确认",@"已确认",@"已收货",@"已取消",@"已完成",@"已作废"] andFont:[UIFont lf_systemFontOfSize:15]];
    self.orderTypeView = orderTypeView;
    orderTypeView.hidden = YES;
    [self.view addSubview:orderTypeView];
    
    [orderTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(typeView.mas_bottom);
        make.left.equalTo(self.view);
        make.width.mas_equalTo((SCREEN_WIDTH-[UIView lf_sizeFromIphone6:30])/3+[UIView lf_sizeFromIphone6:15]);
        make.height.mas_equalTo(0);
    }];
}

-(void)dealloc
{
    [self.popView removeFromSuperview];
}

-(void)racValue
{
    @weakify(self);
    [[self.orderNaviView.groupButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        
        MBGroupOrderViewController* groupOrderView = [[MBGroupOrderViewController alloc]initWithViewModel:[[MBOrderViewModel alloc]init]];
        [self.navigationController pushViewController:groupOrderView animated:YES];
    }];
    
    [RACObserve(self.typeView, orderType)subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        int type = [x intValue];
        if (type == -2 || type == -1) {
            //            全部订单
            if (type == -1) {
                [self showOrderTypeView];
            }else {
                [self showOrderTypeView];
            }
            self.viewModel.param.price = 0;
            self.viewModel.param.order_time = 0;
            
        }else if (type == 1 || type == 2){
            if (type == 1) {
                //                价格升序
                self.viewModel.param.price = 2;
            }else {
                //                价格降序
                self.viewModel.param.price = 1;
            }
            self.viewModel.param.order_time = 0;

        }else if (type == 3 || type ==4){
            if (type == 3) {
                //                时间升序
                self.viewModel.param.order_time = 2;
            }else {
                //                时间降序
                self.viewModel.param.order_time = 1;
            }
            self.viewModel.param.price = 0;
        }
        if (type != -1 && type != -2) {
            [self pullTableViewRequestData:YES];
        }
    }];
    
    [RACObserve(self.viewModel, isNeedReload) subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        if ([x boolValue]) {
            [self.tableView reloadData];
        }
    }];
    
    [RACObserve(self.viewModel, isNeedRefresh) subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        if ([x boolValue]) {
            [self.tableView reloadData];
            [self.popView close];
        }
    }];
    
    [RACObserve(self.viewModel, shippingOrderModel) subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        if (x) {
            self.popView.orderModel = self.viewModel.shippingOrderModel;
            [self.popView show];
        }
    }];
    
    [RACObserve(self.popView, shureSendGoods) subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        if ([x boolValue]) {
//            确认发货
            [self.viewModel sendGoodsDeliverHandleWithParam:self.popView.confirmParam andOrderModel:self.popView.orderModel];
        }
    }];
    
    [RACObserve(self.orderTypeView, groupType) subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        int type = [x intValue];
        if (type != 0) {
            self.viewModel.param.order_status = type -2;
            [self pullTableViewRequestData:YES];
            [self.typeView.selectButton setTitle:self.orderTypeView.typeName forState:UIControlStateNormal];
            [self.typeView.selectButton setImage:[UIImage imageNamed:@"day_un_choose"] forState:UIControlStateSelected];
            self.typeView.orderType = -2;
        }
    }];
}

-(void)hiddenOrderTypeView
{
    [self.typeView.selectButton setImage:[UIImage imageNamed:@"day_un_choose"] forState:UIControlStateSelected];
    self.typeView.orderType = -2;
}

-(void)showOrderTypeView
{
    if (!self.isShowOrderType) {
        [self.orderTypeView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo([UIView lf_sizeFromIphone6:35]*7);
        }];
        self.orderTypeView.hidden = NO;
        self.isShowOrderType = YES;
        self.shadowView.hidden = NO;
    }else {
        self.orderTypeView.hidden = YES;
        self.isShowOrderType = NO;
        self.shadowView.hidden = YES;
        [self.orderTypeView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }
}

#pragma mark - UITextField Delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if ([textField.text clearSpace].length >0) {
        [self.view endEditing:YES];
        self.viewModel.param.keywords = textField.text;
        
        [self pullTableViewRequestData:YES];
    }
    return YES;
}

-(void)setUpData
{
    [self.viewModel sendRequest:^(bool success) {
        [self hideLoadingViewFooter];
        if (success) {
            if (self.orderNaviView.searchTextField.text.length >0) {
                self.viewModel.param.keywords = @"";
                self.orderNaviView.searchTextField.text = @"";
            }
            [self.tableView reloadData];
        }
    }];
}

-(void)pullTableViewRequestData:(BOOL)isRefresh
{
    if (isRefresh) {
        [self.viewModel initRequestPullPage];
    }
    
    [self setUpData];
}

-(Class)cellClassForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [MBOrderGoodsTableViewCell class];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MBBaseTableViewCell *cell = [[self cellClassForRowAtIndexPath:indexPath] cellForTableView:tableView viewModel:[self.viewModel cellViewModelForRowAtIndexPath:indexPath]];
    cell.selectionStyle = [self.viewModel tableViewCellSelectionStyle];
    MBOrderGoodsTableViewCell* goodsCell = (MBOrderGoodsTableViewCell*)cell;
    goodsCell.goodsModel = goodsCell.viewModel.dataModel.goods[indexPath.row];
    return cell;
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self.view endEditing:YES];
}


@end
